#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

#include "oled.h"
#include "logo_bmp.h"
#include "event.h"
#include "wifi.h"

static struct wifi_event_struct wifi = {0};

const uint8_t logo[8][128+1] = {
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF0,0xF8,0x7C,0x7C,0x7E,0x7E,0x7E,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFC,0xFC,0xF8,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xF0,0xF8,0xFE,0xFF,0xFF,0x07,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x28,0x22,0x20,0x18,0x00,0x00,0x00,0x01,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFC,0xF0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFC,0xFC,0xFC,0xFE,0xFE,0xF4,0xF6,0xF6,0x36,0x33,0x73,0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x0F,0x0F,0x3F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0x1F,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF9,0xF0,0xFC,0xFE,0xFE,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x07,0x03,0x81,0xE0,0xF8,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x1F,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0xE0,0xF0,0xF8,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0F,0x1F,0x1F,0x3F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFB,0xF9,0xF9,0xF9,0xF8,0xF9,0xF7,0xF7,0xF3,0xF3,0xF0,0xF3,0xFF,0xFF,0xFF,0x7F,0x7F,0x3F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
};


const uint8_t console_normal[4][32+1] = {
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0xE0,0x10,0x08,0x08,0xC8,0xC8,0x08,0x08,0xC8,0xC8,0x08,0x08,0xC8,0xC8,0x08,0x08,0xC8,0xC8,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x03,0x03,0x00,0x3F,0x40,0x46,0x46,0x40,0x40,0x46,0x46,0x40,0x40,0x46,0x46,0x40,0x40,0x46,0x46,0x40,0x40,0x46,0x46,0x40,0x40,0x3F,0x00,0x03,0x03,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
};

const uint8_t console_inverse[4][32+1] = {
	{0x40,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0xFF,0x1F,0xEF,0xF7,0xF7,0x37,0x37,0xF7,0xF7,0x37,0x37,0xF7,0xF7,0x37,0x37,0xF7,0xF7,0x37,0x37,0xF7,0xF7,0xF7,0xEF,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFC,0xFC,0xFF,0xC0,0xBF,0xB9,0xB9,0xBF,0xBF,0xB9,0xB9,0xBF,0xBF,0xB9,0xB9,0xBF,0xBF,0xB9,0xB9,0xBF,0xBF,0xB9,0xB9,0xBF,0xBF,0xC0,0xFF,0xFC,0xFC,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
};

const uint8_t fm_normal[4][32+1] = {
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x18,0x08,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0xF8,0x04,0xE4,0x94,0x14,0x14,0xD6,0x16,0x16,0xD5,0x15,0x14,0x94,0x14,0x14,0x14,0xD4,0x14,0x14,0xD4,0x14,0x14,0xF4,0x04,0x0C,0xF8,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0xFF,0x00,0x00,0x01,0x01,0x29,0x29,0x29,0x29,0x21,0x01,0x01,0x01,0x01,0x01,0x61,0xB1,0x19,0x09,0x09,0x09,0x91,0xE0,0x00,0x00,0xFF,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x07,0x08,0x18,0x18,0x18,0x19,0x19,0x19,0x19,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x19,0x1B,0x1A,0x1A,0x1B,0x19,0x18,0x08,0x08,0x07,0x00,0x00,0x00},
};

const uint8_t fm_inverse[4][32+1] = {
	{0x40,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0xBF,0xBF,0xDF,0xDF,0xEF,0xEF,0xE7,0xF7,0xF3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0x07,0xFB,0x1B,0x6B,0xEB,0xEB,0x29,0xE9,0xE9,0x2A,0xEA,0xEB,0x6B,0xEB,0xEB,0xEB,0x2B,0xEB,0xEB,0x2B,0xEB,0xEB,0x0B,0xFB,0xF3,0x07,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,0xFE,0xFE,0xD6,0xD6,0xD6,0xD6,0xDE,0xFE,0xFE,0xFE,0xFE,0xFE,0x9E,0x4E,0xE6,0xF6,0xF6,0xF6,0x6E,0x1F,0xFF,0xFF,0x00,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0xF8,0xF7,0xE7,0xE7,0xE7,0xE6,0xE6,0xE6,0xE6,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE6,0xE4,0xE5,0xE5,0xE4,0xE6,0xE7,0xF7,0xF7,0xF8,0xFF,0xFF,0xFF},
};

const uint8_t info_normal[4][32+1] = {
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x60,0x30,0x10,0x10,0x18,0x18,0x18,0x18,0x18,0x18,0x10,0x10,0x30,0x60,0x40,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0xF0,0xFC,0x02,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xCE,0xCE,0xC4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x06,0xFC,0xF0,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x0F,0x3F,0x40,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x7F,0x0F,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x02,0x06,0x0C,0x08,0x08,0x18,0x18,0x18,0x18,0x18,0x18,0x08,0x08,0x0C,0x06,0x02,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00},
};

const uint8_t info_inverse[4][32+1] = {
	{0x40,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0xBF,0x9F,0xCF,0xEF,0xEF,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xEF,0xEF,0xCF,0x9F,0xBF,0x3F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0x0F,0x03,0xFD,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,0x31,0x31,0x3B,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0xF9,0x03,0x0F,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0xF0,0xC0,0xBF,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,0x80,0x80,0x80,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0x80,0xF0,0xFF,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFC,0xFD,0xF9,0xF3,0xF7,0xF7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xF7,0xF7,0xF3,0xF9,0xFD,0xFD,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF},
};

const uint8_t (*normal_table[])[] = {console_normal, fm_normal, info_normal};
const uint8_t (*inverse_table[])[] = {console_inverse, fm_inverse, info_inverse};

const uint8_t select_menu[1][32+1] = {
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};

/* FM Search */
const uint8_t fm_search[5][72+1] = {
	{0X40, 0xFF,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xE1,0xF1,0x39,0x19,0x19,0x19,0x39,0xF1,0xE1,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x03,0xFF},
	{0X40, 0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x06,0x0C,0x0C,0x0C,0x06,0x1F,0x3B,0x70,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x60,0x60,0x00,0x00,0x60,0x60,0x00,0x00,0x60,0x60,0x00,0x00,0x60,0x60,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF},
	{0X40, 0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0x60,0x80,0x00,0x80,0x60,0xF0,0xF0,0x00,0x00,0xC0,0x00,0x00,0x00,0xC0,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0xFF},
	{0X40, 0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F,0x00,0x01,0x03,0x01,0x00,0x1F,0x1F,0x00,0x00,0x1F,0x02,0x02,0x02,0x1F,0x00,0x00,0x18,0x14,0x12,0x11,0x00,0x80,0xFF},
	{0X40, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
};

/* FM MENU */
const uint8_t fm[6][128+1] = {
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0x1C,0xF0,0xC0,0x00,0x00,0xFE,0xFE,0x00,0x00,0xFE,0xFE,0x06,0x06,0x06,0x06,0x06,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x00,0x00,0x03,0x0E,0x38,0x7F,0x7F,0x00,0x00,0x7F,0x7F,0x60,0x60,0x60,0x60,0x60,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x40,0xC0,0x00,0x40,0x40,0xC0,0x00,0x00,0x00,0xC0,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x40,0x80,0x40,0xE0,0x00,0xE0,0x80,0x80,0xE0,0x00,0x40,0x40,0xC0,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x05,0x07,0x00,0x00,0x00,0x07,0x00,0x04,0x00,0x05,0x85,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x00,0x03,0x00,0x00,0x03,0x00,0x02,0x03,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x0F,0x00,0x0F,0x08,0x0F,0x00,0x0F,0x0A,0x0F,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x0E,0x1F,0x3F,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,0x3F,0x1F,0x0E,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
};


/* WIFI MENU */
const uint8_t wifi_logo[2][32+1] = {
	{0x40,0x08,0xF8,0x00,0xF8,0x00,0xF8,0x08,0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00},
	{0x40,0x00,0x03,0x3E,0x01,0x3E,0x03,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00}
};

const uint8_t switch_logo[8][64+1] = {
	/* off */
	{0x40,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0xE0,0xE0},
	{0x40,0xFF,0xFF,0xFF,0x01,0x01,0xF9,0xF9,0xF9,0xF9,0xF9,0x01,0x01,0xFF,0xFF,0x01,0x01,0x79,0x79,0x79,0x79,0xFF,0xFF,0x01,0x01,0x79,0x79,0x79,0x79,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0x06,0x06,0x06,0x06,0x06,0xFE,0xFE,0x00,0x00,0xFE,0xFE,0x18,0x70,0xC0,0x00,0x00,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF},
	{0x40,0xFF,0xFF,0xFF,0x00,0x00,0x3F,0x3F,0x3F,0x3F,0x3F,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFE,0xFE,0xFE,0xFE,0xFF,0xFF,0x00,0x00,0xFE,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xC0,0xC0,0xC0,0xC0,0xC0,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0x03,0x1E,0x30,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF},
	{0x40,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0F,0x0F},
	/* on */
	{0x40,0xE0,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
	{0x40,0xFF,0x00,0x00,0xFE,0xFE,0x06,0x06,0x06,0x06,0x06,0xFE,0xFE,0x00,0x00,0xFE,0xFE,0x86,0x86,0x86,0x86,0x00,0x00,0xFE,0xFE,0x86,0x86,0x86,0x86,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01,0xF9,0xF9,0xF9,0xF9,0xF9,0x01,0x01,0xFF,0xFF,0x01,0x01,0xE7,0x8F,0x3F,0xFF,0xFF,0x01,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
	{0x40,0xFF,0x00,0x00,0xFF,0xFF,0xC0,0xC0,0xC0,0xC0,0xC0,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x01,0x01,0x01,0x01,0x00,0x00,0xFF,0xFF,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x3F,0x3F,0x3F,0x3F,0x3F,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0xFC,0xE1,0xCF,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
	{0x40,0x0F,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F},
};

/* 正在连接... */
const uint8_t wifi_connect[2][88+1] = {
	{0x40,0x00,0x02,0x02,0xC2,0x02,0x02,0x02,0xFE,0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x00,0x08,0x08,0x88,0xC8,0x38,0x0C,0x0B,0x08,0x08,0xE8,0x08,0x08,0x08,0x08,0x08,0x00,0x40,0x40,0x42,0xCC,0x00,0x04,0x44,0x64,0x5C,0x47,0xF4,0x44,0x44,0x44,0x04,0x00,0x10,0x10,0x10,0xFF,0x10,0x50,0x44,0x54,0x65,0xC6,0x44,0x64,0x54,0x44,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x02,0x01,0x00,0xFF,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x00,0x00,0x40,0x20,0x1F,0x20,0x44,0x44,0x44,0x44,0x44,0x7F,0x44,0x44,0x44,0x44,0x00,0x04,0x44,0x82,0x7F,0x01,0x82,0x82,0x4A,0x56,0x23,0x22,0x52,0x4E,0x82,0x02,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00},
};

/* 获取IP ... */
const uint8_t wifi_get_ip[2][80+1] = {
	{0x40,0x04,0x14,0xA4,0x44,0xAF,0x14,0x04,0x04,0x04,0xF4,0x0F,0x24,0x44,0x04,0x04,0x00,0x02,0x02,0xFE,0x92,0x92,0x92,0xFE,0x02,0x06,0xFC,0x04,0x04,0x04,0xFC,0x00,0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x40,0x12,0x49,0x84,0x42,0x3F,0x81,0x41,0x31,0x0D,0x03,0x0D,0x31,0x41,0x81,0x81,0x00,0x08,0x18,0x0F,0x08,0x08,0x04,0xFF,0x04,0x84,0x40,0x27,0x18,0x27,0x40,0x80,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00},
};

const uint8_t wifi_client[2][16+1] = {
	{0x40,0x00,0x00,0xF8,0x04,0x14,0xA4,0x44,0x04,0x04,0x04,0x04,0x04,0x04,0xF8,0x00,0x00},
	{0x40,0x00,0x00,0x03,0x24,0x35,0x3C,0x34,0x3D,0x3D,0x35,0x3D,0x34,0x24,0x03,0x00,0x00},
};

const uint8_t wifi_port[2][16+1] = {
	{0x40,0x00,0x00,0xFC,0x0C,0xFC,0xFC,0x0C,0xFC,0xFC,0x0C,0xFC,0xFC,0x0C,0xFC,0x00,0x00},
	{0x40,0x00,0x00,0x1F,0x3C,0x38,0x30,0x20,0x20,0x20,0x20,0x30,0x38,0x3C,0x1F,0x00,0x00},
};
/* 数字 */
const uint8_t number[14][16] = {
	{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},/*"0",0*/
	{0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00},/*"1",1*/
	{0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},/*"2",2*/
	{0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00},/*"3",3*/
	{0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24},/*"4",4*/
	{0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00},/*"5",5*/
	{0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00},/*"6",6*/
	{0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00},/*"7",7*/
	{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},/*"8",8*/
	{0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00},/*"9",9*/
	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00},/*".",10*/
	{0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00},/*":",11*/
	{0x00,0x00,0x00,0x00,0xC0,0x70,0x18,0x00,0x00,0x20,0x18,0x06,0x01,0x00,0x00,0x00},/*"/",12*/
	{0x00,0x40,0xE0,0x50,0x40,0xF0,0x50,0x40,0x00,0x04,0x1F,0x04,0x04,0x1F,0x04,0x00},/*"#",13*/
};

char* string_to_image(char *str, int len)
{
	int length = 0, offset = 0;
	int half = len * 8 + 1;
	char *buf = str;
	char *image = (char *)malloc(2 * half);
	if (!image)
		return NULL;

	image[0] = 0x40;
	image[half] = 0x40;

	while(len > 0) {
		if (*buf == '.') {
			offset = 10;
		} else if (*buf == ':') {
			offset = 11;
		} else if (*buf == '/') {
			offset = 12;
		} else if ((*buf >= '0') && (*buf <= '9')) {
			offset = *buf - '0';
		} else {
			/* error '#' */
			offset = 13;
		}
		memcpy(&image[length*8 + 1], number[offset], 8);
		memcpy(&image[length*8 + 1 + half], number[offset] + 8, 8);
		length++;
		buf++;
		len--;
	}

	return image;
}

void show_one_menu(uint8_t menu, uint8_t inverse)
{
	int i, offset = 1 + menu * 5;
	const uint8_t (*menu_bmp)[32+1];
	if (inverse)
		menu_bmp = inverse_table[menu];
	else
		menu_bmp = normal_table[menu];

	for(i = 0; i < 4; i++) {
		oled_set_start_address(i + 2, offset * 8);
		oled_write(menu_bmp[i], 32+1);
	}
}

void show_menu(uint8_t current_select, struct event_struct* evt)
{
	int i = 0;
	int offset = 1 +  current_select * 5;

	oled_clear();

	for (i = 0; i < MENU_NUM; i++) {
		show_one_menu(i, current_select == i);
	}
	oled_clear_page(6);
	oled_set_start_address(6, offset * 8);
	oled_write(select_menu[0], 32+1);
}

void show_logo(void)
{
	int i = 0;

	oled_clear();

	for (i = 0; i < 8; i++) {
		oled_set_start_address(i, 0);
		oled_write(logo[i], 128+1);
	}
}

void show_fm_menu(uint8_t current_select, struct event_struct* evt)
{
	int i;
	if (current_select == 0) {
		oled_clear();

		for (i = 0; i < 6; i++) {
			oled_set_start_address(i + 2, 0);
			oled_write(fm[i], 128+1);
		}
	}

	for (i = 0; i < 4; i++) {
		oled_set_start_address(i+2, 32);
		oled_write(switch_logo[current_select * 4 + i], 64+1);
	}
}

char* freq_to_image(uint32_t freq_khz, int *length)
{
	char buf[6] = {0};
	int len;

	/* we need 123.45Mhz*/
	freq_khz /= 10;

	if(freq_khz < 10000) {
		buf[0] = freq_khz / 1000 + '0';
		buf[1] = freq_khz % 1000 / 100 + '0';
		buf[2] = '.';
		buf[3] = freq_khz % 100 / 10 + '0';
		buf[4] = freq_khz % 10 + '0';
		len = 5;
	} else {
		buf[0] = freq_khz / 10000 + '0';
		buf[1] = freq_khz % 10000 / 1000 + '0';
		buf[2] = freq_khz % 1000 / 100 + '0';
		buf[3] = '.';
		buf[4] = freq_khz % 100 / 10 + '0';
		buf[5] = freq_khz % 10 + '0';
		len = 6;
	}

	*length = len;

	return string_to_image(buf, len);
}

void show_fm(uint8_t current_select, struct event_struct* evt)
{
	int i = 0;
	uint32_t freq;
	uint16_t event_type = evt->event_type;

	if ((event_type >> 8) & KEY_EVENTS) {
		printf("key event clear all\n");
		oled_clear();
		for (i = 0; i < 6; i++) {
			oled_set_start_address(i + 2, 0);
			oled_write(fm[i], 128 + 1);
		}
	}

	if ((event_type >> 8) & FM_EVENTS) {
		if (event_type & FM_PLAY) {
			printf("fm event play!\n");
			char *buf;
			int len, offset;
			freq = evt->fm.freq;
			if (freq < 100000)
				offset = 4;
			else
				offset = 0;
			buf = freq_to_image(freq, &len);
			if (buf) {
				for(i = 0; i < 2; i++) {
					oled_set_start_address(2 + i, 44 + offset);
					oled_write((const uint8_t *)&buf[i + len * 8 * i], len * 8 + 1);
				}
				free(buf);
			}
		}

		if (event_type & FM_SEARCH_FINISH) {
			printf("fm event search finish!\n");
			oled_clear();
			for (i = 0; i < 6; i++) {
				oled_set_start_address(i + 2, 0);
				oled_write(fm[i], 128 + 1);
			}
		}

		if (event_type & FM_SEARCH_START) {
			printf("fm event search start!\n");
			for (i = 0; i < 5; i++) {
				oled_set_start_address(i + 2, 24);
				oled_write(fm_search[i], 72 + 1);
			}
		}

		if (event_type & FM_SEARCHING) {
			char *buf;
			int len, offset;
			printf("fm event search ing!\n");
			freq = evt->fm.freq;
			if (freq < 100000)
				offset = 4;
			else
				offset = 0;
			printf("freq = %0.1fMhz\n", (float)(freq) / 1000);
			buf = freq_to_image(freq, &len);
			if (buf) {
				for(i = 0; i < 2; i++) {
					oled_set_start_address(4 + i, 24 + 3 + offset);
					oled_write((const uint8_t *)&buf[i + len * 8 * i], (len - 1) * 8 + 1);
				}
				free(buf);
			}
		}
	}
}

void show_wifi_info(struct event_struct* evt, struct wifi_event_struct *wifi)
{
	int i;
	uint16_t event_type = evt->event_type;

	if ((event_type & 0xff) == 0)
		return;

	if (event_type & WIFI_STA_CONNECTING) {
		/* 正在连接... */
		for (i = 0; i < 2; i++) {
			oled_clear_page(2 + i);
			oled_set_start_address(2 + i, 20);
			oled_write(wifi_connect[i], 88+1);
		}
	}

	if (event_type & WIFI_STA_GET_IP) {
		/* 获取IP ... */
		for (i = 0; i < 2; i++) {
			oled_clear_page(2 + i);
			oled_set_start_address(2 + i, 20);
			oled_write(wifi_get_ip[i], 80+1);
		}
	}

	if (event_type & WIFI_STA_DISCONNECTED) {
		if (wifi)
			wifi->is_connect = 0;
	}

	if (event_type & WIFI_STA_CONNECTED) {
		int len;
		char *buf;
		/* 显示IP地址 */
		len = strlen(evt->wifi.ip);
		buf = string_to_image(evt->wifi.ip, len);
		if (buf) {
			for (i = 0; i < 2; i++) {
				oled_clear_page(2 + i);
				oled_set_start_address(2 + i, 0);
				oled_write((const uint8_t *)&buf[i + len * 8 * i], len * 8 + 1);
			}
			free(buf);
		}

		if (wifi) {
			wifi->is_connect = 1;
			memset(wifi->ip, 0, 16);
			memcpy(wifi->ip, evt->wifi.ip, len);
		}

		/* 显示客户端图片 */
		for (i = 0; i < 2; i++) {
			oled_clear_page(4 + i);
			oled_set_start_address(4 + i, 0);
			oled_write(wifi_client[i], 16 + 1);
		}

		/* 显示端口图片 */
		for (i = 0; i < 2; i++) {
			oled_clear_page(6 + i);
			oled_set_start_address(6 + i, 0);
			oled_write(wifi_port[i], 16 + 1);
		}

		/* 显示客户端连接数量 */
		len = strlen("0/3");
		buf = string_to_image("0/3", len);
		if (buf) {
			for (i = 0; i < 2; i++) {
				oled_set_start_address(4 + i, 24);
				oled_write((const uint8_t *)&buf[i + len * 8 * i], len * 8 + 1);
			}
			free(buf);
		}
		int port = PORT;
		char str_port[5];
		memset(str_port, 0, 5);
		i = 0;
		str_port[i++] = port / 1000 + '0';
		str_port[i++] = port % 1000 / 100 + '0';
		str_port[i++] = port % 100 / 10 + '0';
		str_port[i++] = port % 10 + '0';
		len = i;
		buf = string_to_image(str_port, len);
		if (buf) {
			for (i = 0; i < 2; i++) {
				oled_set_start_address(6 + i, 24);
				oled_write((const uint8_t *)&buf[i + len * 8 * i], len * 8 + 1);
			}
			free(buf);
		}
	}

	if (event_type & (WIFI_CLIENT_CONNECTED | WIFI_CLIENT_DISCONNECTED)) {
		int client_num = evt->wifi.client_num;
		char str_client[2] = {0};
		int len;
		char *buf;
		if (wifi)
			wifi->client_num = client_num;
		if (client_num > MAX_CLIENT_NUM)
			str_client[0] = '#';
		else
			str_client[0] = client_num + '0';

		len = 1;
		buf = string_to_image(str_client, len);
		if (buf) {
			for (i = 0; i < 2; i++) {
				oled_set_start_address(4 + i, 24);
				oled_write((const uint8_t *)&buf[i + len * 8 * i], len * 8 + 1);
			}
			free(buf);
		}
	}
}


/*
 * +----------------+
 * |                |
 * |###.# ON|OFF ## |
 * |C  0/3          |
 * |P  3333         |
 * +----------------+
 */

void show_wifi_menu(uint8_t current_select, struct event_struct* evt)
{
	int i;
	struct event_struct new_evt = {0};
	oled_clear();
	if (wifi.is_connect == 0) {
		memcpy(new_evt.wifi.ip, "###.###.###.###", 15);
	} else {
		memcpy(&new_evt.wifi, &wifi, sizeof(wifi));
	}
	new_evt.event_type = WIFI_STA_CONNECTED | WIFI_CLIENT_CONNECTED;
	show_wifi_info(&new_evt, NULL);

	for (i = 0; i < 4; i++) {
		oled_set_start_address(2 + i, 32);
		oled_write(switch_logo[current_select*4 + i], 64+1);
	}
}

/*
 * +----------------+
 * |                |
 * |192.168.12.123  |
 * |C  0/3          |
 * |P  3333         |
 * +----------------+
 */
void show_wifi(uint8_t current_select, struct event_struct* evt)
{
	/* find wifi sub events */
	uint16_t event_type = evt->event_type;
	if ((event_type >> 8) != WIFI_EVENTS)
		return;
	printf("wifi event_type 0x%02x\n", event_type);

	if (event_type & 0xff) {
		show_wifi_info(evt, &wifi);
	}

	if (wifi.is_connect) {
		struct event_struct new_evt = {0};
		memcpy(&new_evt.wifi, &wifi, sizeof(wifi));
		new_evt.event_type = WIFI_STA_CONNECTED | WIFI_CLIENT_CONNECTED;
		show_wifi_info(&new_evt, NULL);
	}
}
